package logic

import (
	"context"
	"yuni/model/mysql"
	"yuni/yunirpc/internal/svc"
	"yuni/yunirpc/yunirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectListLogic {
	return &CollectListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectListLogic) CollectList(in *yunirpc.CollectListRequest) (*yunirpc.CollectListResponse, error) {
	// todo: add your logic here and delete this line

	var m []mysql.UserData
	mysql.Db.Debug().Model(&mysql.Product{}).
		Joins("left join users on products.user_id = products.id").
		Joins("left join classes on products.class_id = classes.id").
		Find(&m)

	var an []*yunirpc.CollectList
	for _, i2 := range m {
		list := yunirpc.CollectList{
			Title:   i2.Title,
			Price:   float32(i2.Price),
			Stock:   i2.Stock,
			UserId:  i2.UserId,
			ClassId: i2.ClassId,
		}
		an = append(an, &list)
	}

	return &yunirpc.CollectListResponse{
		List: an,
	}, nil
}
